﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data.Linq.SqlClient;

/// <summary>
///TouShu 的摘要说明
/// </summary>
public partial class TouShu
{

    public enum type { 个人培训 = 1, 企业培训 = 2, 个人辅导班 = 3, 个人辅导一对一 = 4 }
    public enum status { 处理中 = 1, 投诉成功 = 2, 投诉失败 = 3 }
    public static TouShu GetByID(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.TouShu.FirstOrDefault(row => row.ID == id && row.Enable_Flag == 1);
        }
    }


    public static List<TouShu> GetAllByConditon(int Type, int GetUserID, int Status, string AddTimeForm, string AddTimeTo, int AddUserID, string Key)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.TouShu
                        where t.Enable_Flag == 1
                        select t;
            if (Type != 0)
                query = query.Where(row => row.Type == Type);
            if (GetUserID != 0)
                query = query.Where(row => row.GetUserID == GetUserID);
            if (Status != 0)
                query = query.Where(row => row.Status == Status);
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= DongDong.Common.Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= DongDong.Common.Misc.ConvertToDateTime(AddTimeTo));
            if (AddUserID != 0)
                query = query.Where(row => row.RelationUserID == AddUserID);
            if (!string.IsNullOrEmpty(Key))
                query = query.Where(row => row.Des.Contains(Key));
            query = query.OrderByDescending(row => row.AddTime);
            return query.ToList();
        }
    }



    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.TouShu.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<TouShu>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }


    public static void BadDelete(int ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            TouShu objTouShu = TouShu.GetByID(ID);
            if (objTouShu == null)
                return;
            objTouShu.Enable_Flag = 0;
            objTouShu.Update();
        }
    }


    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.TouShu.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.TouShu.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }
}